<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="mysql," />










<meta name="description" content="常用索引场景 主键索引 和唯一索引相同的是索引列的值唯一，但是索引列的值不可以为null。   普通索引 给某个列添加索引，这个就不需要多说了。   唯一索引 顾名思义，索引列的值唯一，但索引列的值可以为null。   联合索引  使用多个列联合创建索引，根据mysql“最左前缀原则“， 按照经常使用的查询条件顺序建立索引，例如一个表“user”中: id，name，age三个列建立联合索引  1">
<meta name="keywords" content="mysql">
<meta property="og:type" content="article">
<meta property="og:title" content="记录mysql关于索引和sql优化的学习">
<meta property="og:url" content="http://yoursite.com/2018/08/24/记录mysql关于索引和sql优化的学习/index.html">
<meta property="og:site_name" content="zqx的个人小站">
<meta property="og:description" content="常用索引场景 主键索引 和唯一索引相同的是索引列的值唯一，但是索引列的值不可以为null。   普通索引 给某个列添加索引，这个就不需要多说了。   唯一索引 顾名思义，索引列的值唯一，但索引列的值可以为null。   联合索引  使用多个列联合创建索引，根据mysql“最左前缀原则“， 按照经常使用的查询条件顺序建立索引，例如一个表“user”中: id，name，age三个列建立联合索引  1">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-08-24T15:18:43.940Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="记录mysql关于索引和sql优化的学习">
<meta name="twitter:description" content="常用索引场景 主键索引 和唯一索引相同的是索引列的值唯一，但是索引列的值不可以为null。   普通索引 给某个列添加索引，这个就不需要多说了。   唯一索引 顾名思义，索引列的值唯一，但索引列的值可以为null。   联合索引  使用多个列联合创建索引，根据mysql“最左前缀原则“， 按照经常使用的查询条件顺序建立索引，例如一个表“user”中: id，name，age三个列建立联合索引  1">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/08/24/记录mysql关于索引和sql优化的学习/"/>





  <title>记录mysql关于索引和sql优化的学习 | zqx的个人小站</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">zqx的个人小站</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/24/记录mysql关于索引和sql优化的学习/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zqx">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/hiei.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zqx的个人小站">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">记录mysql关于索引和sql优化的学习</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-24T22:13:55+08:00">
                2018-08-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/mysql/" itemprop="url" rel="index">
                    <span itemprop="name">mysql</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="常用索引场景"><a href="#常用索引场景" class="headerlink" title="常用索引场景"></a>常用索引场景</h2><ul>
<li>主键索引<blockquote>
<p>和唯一索引相同的是索引列的值唯一，但是索引列的值不可以为null。</p>
</blockquote>
</li>
<li>普通索引<blockquote>
<p>给某个列添加索引，这个就不需要多说了。</p>
</blockquote>
</li>
<li>唯一索引<blockquote>
<p>顾名思义，索引列的值唯一，但索引列的值可以为null。</p>
</blockquote>
</li>
<li><p>联合索引</p>
<blockquote>
<p>使用多个列联合创建索引，根据mysql“最左前缀原则“， 按照经常使用的查询条件顺序建立索引，例如一个表“user”中: id，name，age三个列建立联合索引</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 可以应用索引</span><br><span class="line">select xxx from user where id = xxx</span><br><span class="line"># 可以应用索引</span><br><span class="line">select xxx from user where id = xxx and name = xxx</span><br><span class="line"># 可以应用索引</span><br><span class="line">select xxx from user where id = xxx and name = xxx and age = xxx</span><br><span class="line"># 不能应用索引</span><br><span class="line">select xxx from user where name = xxx and id = xxx</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ul>
<li><p>前缀索引</p>
<blockquote>
<p>给某个列添加前缀索引，例如想要对某个列前几个字符进行匹配，可以给这个列的前几个字符添加索引，例如: 某个列的数据是 “gy111”, “gy222”, “gy333”, “xy111”, “xy222”, “xxxxx”。<br>我们可以给这个列的前两个字符添加索引</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select &apos;columeName&apos; from &lt;table&gt; where name like &apos;gy%&apos;</span><br></pre></td></tr></table></figure>
</li>
<li><p>覆盖索引</p>
<blockquote>
<p>这里找到一个解释:  覆盖索引是非聚集组合索引的一种形式，它包括在查询里的Select、Join和Where子句用到的所有列（即建立索引的字段正好是覆盖查询语句[select子句]与查询条件[Where子句]中所涉及的字段，也即，索引包含了查询正在查找的所有数据）。例如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># name 为索引列</span><br><span class="line">select name from &lt;table&gt; where name = ?</span><br></pre></td></tr></table></figure>
</blockquote>
</li>
</ul>
<h2 id="关于一些简单的sql优化"><a href="#关于一些简单的sql优化" class="headerlink" title="关于一些简单的sql优化"></a>关于一些简单的sql优化</h2><ul>
<li><p>避免使用select * 查询</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 在查询语句中要尽量避免使用 select * 查询，明确指定要查询的列</span><br><span class="line">select xxx, yyy, zzz from &lt;table&gt; where xxx = ?</span><br></pre></td></tr></table></figure>
</li>
<li><p>count(*), count(1), count(col)</p>
<blockquote>
<p>count(*) 和 count(1) 基本是一样的，会自动选择not null列，字段较窄的列进行统计；而count(col) 是不会对col值为null的行进行统计，统计所有记录时结果会出现错误，如果col没有索引或不是最优索引，效率也会降低。</p>
</blockquote>
</li>
<li>类型自动转换的问题<blockquote>
<p>例如:一个表”user”中有”name“列为索引列</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 如果有一个语句如下，因为这里条件没有加引号，就会触发类型自动转换，引发全表查询，效率会很低！</span><br><span class="line">select &apos;columeName&apos; from user where name = xxx</span><br><span class="line"># 而加了引号就会触发索引，效率很高</span><br><span class="line">select &apos;columeName&apos; from user where name = &apos;xxx&apos;</span><br></pre></td></tr></table></figure></blockquote>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/mysql/" rel="tag"># mysql</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/15/关于vue框架vue-router使用中的一点小总结/" rel="next" title="关于vue框架vue-router使用中的一点小总结">
                <i class="fa fa-chevron-left"></i> 关于vue框架vue-router使用中的一点小总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/hiei.png"
                alt="zqx" />
            
              <p class="site-author-name" itemprop="name">zqx</p>
              <p class="site-description motion-element" itemprop="description">zqx的个人小站</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#常用索引场景"><span class="nav-number">1.</span> <span class="nav-text">常用索引场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关于一些简单的sql优化"><span class="nav-number">2.</span> <span class="nav-text">关于一些简单的sql优化</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zqx</span>

  
</div>








  <div class="footer-custom">Hosted by <a target="_blank" href="https://pages.github.com">GitHub Pages</a></div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
